// pages/scannCode/scannCode.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageList:[
      "../../images/20221208/3110221208142039_02.png",
      "../../images/20221208/3110221208142039_03.png",
      "../../images/20221208/3110221208142039_04.png",
    ],
    code:''
  },
    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let scannCode =  options.page;
    let token = wx.getStorageSync('token');
    const util = require("../../utils/util");
    let that = this;
    if(!token){
      wx.login({
        success: (res) => {
          console.log(res);
          let code = res.code;
          util.setCode(code);
          wx.setStorageSync('code', code)
          that.setData({
            code
          })
            //没有用户信息
            wx.showModal({
              title: '授权',
              content: '小程序要取得您的微信头像和昵称，请点【确定】继续',
              complete: async (res) => {
                //取消
                if (res.cancel) {
      
                }
                //确定
                if (res.confirm) {
                  //取得用户信息
                  await that.getUserInfo();
                   //登录服务器
                  //that.loginServer();
                  //跳转
                  //  that.jumpToPage();
                }
              }
            })
        },
      })
    }
   
          if(scannCode == 'scannCode'){
            wx.redirectTo({
              url: '/pages/codeing/codeing',
            })
          }else{
            wx.redirectTo({
              url: '/pages/index/index',
            })
          }

  },
  async getUserInfo() {
    let that = this;
    console.log(66);
     wx.getUserProfile({
      desc: '运行联合宝宝',
      success: function (e) {
       // console.log("success,e", e)
        let userInfo = e.userInfo;
          //存到本面变量
          that.setData({
            nickname: userInfo.nickName,
            head_image: userInfo.avatarUrl
          })
        //存到本地缓存
        wx.setStorageSync('userInfo', userInfo)
        //存到全局变量
        const app = getApp();
        app.globalData.userInfo = userInfo;
        const util = require("../../utils/util")
        const api = require("../../api/api")
        let nickname = wx.getStorageSync('userInfo').nickName;
        let head_image = wx.getStorageSync('userInfo').avatarUrl;
        api.user.loginByWechatCode({
          code: that.data.code,
          channel: that.data.channel,
          hash: that.data.hash,
          nickname,
          head_image,
          success(res) {
            // console.log("afterServiceOrderItem,orderDetail,res", res)
            let data = res.data.data;
            if(res.data.code == 0){
              that.setData({
                token: data.token,
              })
              wx.setStorageSync('token', data.token);
              wx.setStorageSync('openid', data.openid);

            }
          }
        });
        // console.log("app.globalData", app.globalData)
        //跳转
        // that.jumpToPage();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})